package com.whity.towerdefense.view;

import java.util.Vector;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.whity.towerdefense.Utils;
import com.whity.towerdefense.model.GameModel;


import android.opengl.GLSurfaceView;

public class ActionPhaseRenderer implements GLSurfaceView.Renderer
{

	// =============================================================
	// Protected attributs
	
	protected final Vector<GraphicDrawable> 	m_GraphicElts;
	
	// =============================================================
	// Public functions :
	
	public ActionPhaseRenderer()
	{
		m_GraphicElts = new Vector<GraphicDrawable>(100);
	}
	
	public void onDrawFrame(GL10 p_GL)
	{
		p_GL.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		
		for(GraphicDrawable elt : m_GraphicElts)
		{
			elt.Draw(p_GL);
		}
	}

	public void onSurfaceChanged(GL10 p_GL, int p_W, int p_H)
	{
		p_GL.glViewport(0, 0, p_W, p_H);
		Utils.RESOLUTION_X = p_W;
		Utils.RESOLUTION_Y = p_H;
		Utils.SCREEN_RATIO = (float)p_W/p_H;
		
		p_GL.glMatrixMode(GL10.GL_PROJECTION);
		p_GL.glLoadIdentity();
		p_GL.glOrthof(0.0f, Utils.SCREEN_RATIO, 0.0f, 1.0f, 0.0f, 1.0f);
		
		// Set default zoom level for the map
		//GraphicElt mapElt = Level.GetMap().GetGraphic();
		//float mapRatio = mapElt.m_Width / mapElt.m_Height;
		/*
		if(mapRatio > Utils.SCREEN_RATIO)
		{
			// Width limited
			float SmallestScale = Utils.RESOLUTION_X/mapElt.m_Width;
			mapElt.SetScale(SmallestScale);
		}
		else
		{
			// Height limited
			float SmallestScale = Utils.RESOLUTION_Y/mapElt.m_Height;
			mapElt.SetScale(SmallestScale);
		}*/
	}

	public void onSurfaceCreated(GL10 p_GL, EGLConfig p_Config)
	{
		p_GL.glClearColor(0.0f, 0.0f, 0.1f, 0.0f);
		
		// OpenGL culling mode :
		p_GL.glFrontFace(GL10.GL_CCW);
		p_GL.glEnable(GL10.GL_CULL_FACE);
		p_GL.glCullFace(GL10.GL_BACK);
		
		// Transparence
		p_GL.glEnable (GL10.GL_BLEND);
		p_GL.glBlendFunc (GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		
		// Load game graphism
		GameModel.LEVEL.LoadGraphism(p_GL);
	}

	// =============================================================
}
